﻿using Entity.Customer;
using Entity.ResultModel.Customer;
using Hotel.Models;
using IHotelBLL.ICustomerBLL;
using IHotelBLL.IRoomBLL;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace Hotel.Controllers.Customer
{
    public class CustomerController : Controller
    {

        ICustomerBLL _customerBLL;
        IRoomInfoBLL _roomInfoBLL;
        public CustomerController(ICustomerBLL customerBLL, IRoomInfoBLL roomInfoBLL)
        {
            _customerBLL = customerBLL;
            _roomInfoBLL = roomInfoBLL;
        }
        //返回对象
        ResultModel res = new ResultModel();
        /// <summary>
        /// 获取用户列表数据接口
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public IActionResult GetCustomer(int page, int limit, string name)
        {
            int count;
            List<GetCustomerInfoModel> customerInfo = _customerBLL.GetCustomer(page, limit, name, out count);

            var res = new
            {
                code = 0,
                msg = "",
                count = count,
                data = customerInfo
            };

            return Json(res);
        }

        public IActionResult AddCustomer(string name,string sex,string iD_Card,string phone)
        {
            //返回对象
            ResultModel res = new ResultModel();
            //参数验证
            if (string.IsNullOrEmpty(name))
            {
                res.Msg = "名字不能为空";
                return Json(res);
            }

            string msg;
            //创建
            bool isSuccess = _customerBLL.AddCustomer(name, sex, iD_Card , phone,out msg);
            if (isSuccess)
            {
                res.Status = 1;
            }
            res.IsSuccess = isSuccess;
            res.Msg = msg;
            return Json(res);
        }


        /// <summary>
        /// 修改更新顾客
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="sex"></param>
        /// <param name="phone"></param>
        /// <param name="address"></param>
        /// <param name="customerAddress"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateCustomerInfo(string id, string name, string sex, string phone, string address)
        {

            if (string.IsNullOrEmpty(id))
            {
                res.Msg = "id不能为空";
                return Json(res);
            }
            if (string.IsNullOrEmpty(name))
            {
                res.Msg = "姓名不能为空";
                return Json(res);
            }

            string msg;
            //调用bll修改用户业务方法
            bool isSuccess = _customerBLL.UpdateCustomerInfo(id, name, sex, phone, address, out msg);

            if (isSuccess)
            {
                res.Status = 1;
                res.IsSuccess = true;
            }
            res.Msg = msg;
            return Json(res);
        }

        public IActionResult GetCustomerInfoById(string id)
        {

            if (string.IsNullOrEmpty(id))
            {
                res.Msg = "id不能为空";
                return Json(res);
            }

            CustomerInfo customerInfo = _customerBLL.GetCustomerInfoById(id);
            if (customerInfo == null)
            {
                res.Msg = "未查询到用户信息";
                return Json(res);
            }
            res.Msg = "成功";
            res.Status = 1;
            res.Data = customerInfo;

            res.IsSuccess = true;
            return Json(res);

        }

        //房间号下拉选
        public IActionResult GetRoomInfoOptions()
        {
            res.Data = _roomInfoBLL.GetRoomInfoOptions();
            res.Status = 1;
            res.IsSuccess = true;
            if (res.Data is null)
            {
                res.Msg = "未找到房间号";
                res.IsSuccess = false;
            }
            return Json(res);
        }
    }
}